package com.plugins.activity;


import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.alibaba.fastjson.JSON;
import com.plugins.pluginlib.IBasePluginActivity;
import com.plugins.pluginlib.broadcast.BroadMessage;
import com.plugins.test.R;

public class TestActivity extends IBasePluginActivity {
    private TextView tvMsg;
    private Button btnSend;
    private EditText etText;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_test);
        tvMsg = findViewById(R.id.tv_text);
        btnSend = findViewById(R.id.btn_send);
        etText = findViewById(R.id.et_text);
        btnSend.setOnClickListener(view -> {
            String text = etText.getText().toString();
            BroadMessage broadMessage = new BroadMessage();
            broadMessage.setCommand("来自插件的消息");
            broadMessage.setData(text);
            sendMessageToHost(broadMessage);
            etText.setText("");
            Toast.makeText(that,"已发送",Toast.LENGTH_SHORT).show();
        });
    }

    @Override
    public void fromHostMessage(BroadMessage message) {
        Log.d("fromHostMessage",JSON.toJSONString(message));
        String msg = "来自宿主程序传递过来的参数：";
        msg+="command:"+message.getCommand()+"\n";
        msg+="status:"+message.getStatus()+"\n";
        msg+="message:"+message.getMessage()+"\n";
        msg+="param:"+ JSON.toJSONString(message.getParam())+"\n";
        msg+="result:"+JSON.toJSONString(message.getResult())+"\n";
        msg+="data:"+JSON.toJSONString(message.getData())+"\n";
        tvMsg.setText(msg);
    }
}